package prv.pursuivant.render.html;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import prv.pursuivant.config.impl.XStreamCategorySerializer;
import prv.pursuivant.config.impl.XStreamCategorySerializerModule;
import prv.pursuivant.core.AchievType;
import prv.pursuivant.core.AchievTypesTree;
import prv.pursuivant.render.OutputGenerator;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.internal.Lists;

public class HtmlTreeRenderingTest {

	private final XStreamCategorySerializer serializer;
	private ArrayList<String> activeAchievs;

	public HtmlTreeRenderingTest(XStreamCategorySerializer serializer) {
		this.serializer = serializer;
		activeAchievs = Lists.newArrayList("168", "168.Emblems",
				"168.Classic Dungeon", "1283", "168.Classic Raid", "1285",
				"168.The Burning Legion Dungeon", "1284",
				"168.The Burning Legion Hero", "1287",
				"168.The Burning Legion Raid", "1286", "168.Lich King Dungeon",
				"1288", "2018", "168.Lich King Hero", "1289", "2136", "1658",
				"2019", "168.Lich King Raid", "2137",
				"168.Lich King Heroic Raid", "2138",
				"168.Secrets of Ulduar Raid", "2957",
				"168.Secrets of Ulduar Heroic Raid", "2958");
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws XMLStreamException
	 */
	public static void main(String[] args) throws IOException,
			XMLStreamException {
		Injector injector = Guice
				.createInjector(new XStreamCategorySerializerModule());
		XStreamCategorySerializer serializer = injector
				.getInstance(XStreamCategorySerializer.class);
		HtmlTreeRenderingTest test = new HtmlTreeRenderingTest(serializer);
		test.test();
		test.testFromRun();
		test.testFromUgly();
	}

	private void test() throws IOException, XMLStreamException {
		File file = new File("testWD/config/category.168.xml");
		FileInputStream fis = new FileInputStream(file);
		AchievTypesTree tree = serializer.readTree(fis);
		fis.close();
		checkTree(tree, tree.getRootId(), "");
		HtmlPageDecorationRenderer renderer = new HtmlPageDecorationRenderer(
				tree, activeAchievs, null);
		FileWriter fw = new FileWriter("testWD/renders/tree.html");
		XMLStreamWriter writer = XMLOutputFactory.newInstance()
				.createXMLStreamWriter(fw);
		OutputGenerator generator = new XHtmlGenerator(writer);
		renderer.renderAchievList(generator, tree.getRoot());
		writer.flush();
		fw.close();
	}

	private void testFromRun() throws IOException, XMLStreamException {
		File file = new File("runWD/config/category.168.xml");
		FileInputStream fis = new FileInputStream(file);
		AchievTypesTree tree = serializer.readTree(fis);
		fis.close();
		checkTree(tree, tree.getRootId(), "");
		HtmlPageDecorationRenderer renderer = new HtmlPageDecorationRenderer(
				tree, activeAchievs, null);
		FileWriter fw = new FileWriter("testWD/renders/tree.fromRun.html");
		XMLStreamWriter writer = XMLOutputFactory.newInstance()
				.createXMLStreamWriter(fw);
		OutputGenerator generator = new XHtmlGenerator(writer);
		renderer.renderAchievList(generator, tree.getRoot());
		writer.flush();
		fw.close();
	}

	private void testFromUgly() throws IOException, XMLStreamException {
		File file = new File("testWD/config/category.fromUgly.168.xml");
		FileInputStream fis = new FileInputStream(file);
		AchievTypesTree tree = serializer.readTree(fis);
		fis.close();
		checkTree(tree, tree.getRootId(), "");
		HtmlPageDecorationRenderer renderer = new HtmlPageDecorationRenderer(
				tree, activeAchievs, null);
		FileWriter fw = new FileWriter("testWD/renders/tree.fromUgly.html");
		XMLStreamWriter writer = XMLOutputFactory.newInstance()
				.createXMLStreamWriter(fw);
		OutputGenerator generator = new XHtmlGenerator(writer);
		renderer.renderAchievList(generator, tree.getRoot());
		writer.flush();
		fw.close();
	}

	private void checkTree(AchievTypesTree tree, String id, String stack) {
		AchievType achiev = tree.findAchievTypeById(id);
		if (achiev.getCriteria().size() > 30)
			System.out.println(stack + achiev + " (" + achiev.getName()
					+ ") has " + achiev.getCriteria().size() + " criteria");
		for (String crit : achiev.getCriteria()) {
			checkTree(tree, crit, stack + "  ");
		}
	}

}
